package com.home.base.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.home.biz.PhoneBiz;
import com.home.entity.GoodsInfo;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;


/**
 * @author ：chenxf
 * @date ：Created in 2019/6/14 9:30
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
public class FetchUtil implements Callable<String> {
    private static Map<String,String> cookie=new HashMap<>();

    private PhoneBiz phoneBiz;
   static String url="https://rwk.cmicrwx.cn/rwx/rwkweb/livehk/livehkMobile/selectNumber";
   static String refer="http://wap.cq.10086.cn/openh5/static/numberMall/numberOrderNew";
    String data="", provName="", cityName="";

   public FetchUtil(PhoneBiz phoneBiz,String provName,String cityName,String data){
       this.data=data;
       this.phoneBiz=phoneBiz;
       this.provName=provName;
       this.cityName=cityName;
   }

   public void save() {

       try {
           phoneBiz.saveList(provName,cityName,"移动花卡宝藏版",fetch());
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
    public List<String> fetch() throws Exception {
        log.debug("fetch data"+data);
        String doc = Jsoup.connect(url)

                .requestBody(data)
                .ignoreContentType(true)
                .method(Connection.Method.POST)
                .header("Content-Type","application/json")
                .header("Referer",refer)
                .userAgent("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)")
                .cookies(cookie)
                .execute()
                .body();
        JSONObject map=JSON.parseObject(doc);
        if("0".equals(map.get("hRet").toString())){
            log.debug("fetch finished");
            return  JSON.parseArray(map.get("numbers").toString(),String.class);

        }else {
            log.error("采集异常");
        }
        return null;
    }

   @Override
    public String call() throws Exception {
        save();
        return data+"fetch finish";
    }




    public static void main(String[] args){
//        try {
//            String data="{\"MsgType\":\"LiveHKSelectNumberReq\",\"Version\":\"1.0.0\",\"provCode\":\"100\",\"cityCode\":\"100\",\"selecttype\":0,\"searchkey\":\"\",\"count\":10}";
//
//           // List<String> goodsInfos=FetchUtil.sjData(data);
//           // System.out.println(goodsInfos.size());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
}
